<template>
	<view class="container">

		<!-- 媒体部分 -->
		<view class="shipinpart">
			<view class="shipinpart-media">
				<u-swiper :list="courseData.course_image" keyName="uri" height="220" radius="0" indicator
					indicatorMode="line" circular></u-swiper>
			</view>
			<view class="shipinpart-info">
				<text class="info-span1">{{courseData.name}}</text>
				<view class="info-span2">
					<text>{{courseData.synopsis}}</text>
					<text>更新中</text>
				</view>
				<view class="info-span5">
					<text class="info-span3">
						<text>{{courseData.course_status}}人学过</text>
					</text>
					<text class="info-span4">￥{{courseData.sell_price}}</text>
				</view>
			</view>
		</view>

		<!-- 课程介绍、课程目录、附件、用户评价 -->
		<view class="kechengpart">
			<view class="kechengpart-title">
				<view class="kechengpart-title-item" v-for="(item, index) in kechengList" :key="index">
					<view @click="muluchange(index)" :class="{btna:btnnum == index}">{{item}}</view>
					<text :class="{_underline: btnnum == index}"></text>
				</view>
			</view>
			<view class="kechengpart-content">

				<!-- 课程介绍 -->
				<view class="kcjs" :class="{dis:btnnum == 0}" :model="teacher">
					<view class="kcjs-brief">
						<view class="kcjs-brief-center">
							<u-parse :content="courseData.content"></u-parse>
						</view>
					</view>
				</view>


				<!-- 课程目录 -->
				<view class="kcml" style="margin-bottom: 122upx;" :class="{dis:btnnum == 1}">
					<view class="kcml-list" v-for="(item,index) in catalogue_list
" :key="item.id" @click="openvideo(item.id)">
						<view class="kcml-list-left">
							<text>{{(index+1)+' . '+ item.name}}</text>
						</view>
						<view class="kcml-list-right">
							<view v-if="item.fee_type == 0 ">
								<text v-if="type === 0">试读</text>
								<text v-else-if="type === 1">试听</text>
								<text v-else-if="type === 2">试看</text>
								<text v-else>试试看</text>
							</view>
							<image v-else
								src="https://jiangxinsaas.oss-cn-beijing.aliyuncs.com/tuchuang/471710857207Bi1rOW.png"
								mode=""></image>
						</view>
					</view>
				</view>



				<!-- 用户评价 -->
				<!-- <view class="yhpj" :class="{dis:btnnum == 2}">
					<view class="yhpj-list" v-if="comment.length > 0">
						<view class="item" v-for="(item, index) in comment" :key="index">
							<image class="item-left" :src="item.avatar" mode="aspectFit"></image>
							<view class="item-right">
								<view class="item-right-top">
									<text>{{item.nickname}}</text>
									<text>{{item.addtime}}</text>
								</view>
								<view class="item-right-bottom">
									<text>{{item.comment}}</text>
								</view>
							</view>
						</view>
					</view>
					<image v-else class="nopl" src="../../static/nopl.png" mode="aspectFit"></image>
				</view> -->

			</view>

		</view>

		<!-- 立即购买 -->
		<view class="buy">
			<view class="buy-left" @click="collect(0)" v-if="courseData.is_collect">
				<image src="https://jiangxinsaas.oss-cn-beijing.aliyuncs.com/tuchuang/101710856750PypyRb.png" mode="">
				</image>
				<text>取消收藏</text>
			</view>
			<view class="buy-left" @click="collect(1)" v-else>
				<image src="https://jiangxinsaas.oss-cn-beijing.aliyuncs.com/tuchuang/101710856750PypyRb.png" mode="">
				</image>
				<text>收藏</text>
			</view>
			<button class="buy-left sharebtn" open-type="share">
				<image src="https://jiangxinsaas.oss-cn-beijing.aliyuncs.com/tuchuang/171710856757wuuJE4.png" mode="">
				</image>
				<text style="color: #848484;">分享</text>
			</button>
			<view class="buy-right" @click="ShowPay()" v-if="courseData.course_status != 3">立即购买</view>
			<view class="buy-right" @click="openvideo(catalogue_list[0].id)" v-else>去观看</view>
		</view>
		
		<!-- 支付pop -->
		<u-popup :show="paypop" mode="bottom" :round="15" @close="closespaypop" @open="openpaypop">
			<view style="padding-bottom: 30rpx;height: 350px;" class="showbottom">
				<view class="pay-type-list">
					<view class="pay-title">请选择支付方式：</view>
					<radio-group class="uni-flex" name="gender">
						<block v-for="(item, index) in payList" :key="index">
							<view class="type-item" :class="{ 'act': index === selectedIndex }" @click="selectNav(item,index)">
								<image :src="item.icon" mode="" class="a-img"></image>
								<view class="con">
									<text class="tit">{{item.pay_way_name}}</text>
								</view>
							</view>
						</block>
					</radio-group>
				</view>
				<view class="botton" @click="topay()">
					<text class="text110">确认支付</text>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import $H from '@/common/js/request.js';
	export default {
		data() {
			return {
				id: '',
				kechengList: ['课程介绍', '目录', '评价'],
				courseData: [],
				btnnum: 0,
				type:0,
				paypop: false,
				payList: uni.getStorageSync('pay'),
				Payinfo: {},
				pay_way: 1,
				selectedIndex: 10,
				miniapp_audit: uni.getStorageSync('miniapp_audit'),
			}
		},
		onLoad(opt) {
			this.id = opt.id
		},
		onShow() {
			this.IndexData()
		},
		methods: {
			closespaypop(){
				this.paypop = false
			},
			openpaypop(){},
			selectNav(item, index) {
				this.selectedIndex = index;
				this.pay_way = item.pay_config_id
			},
			ShowPay(){
				this.paypop = true
			},
			async IndexData() {
				try {
					const res = await $H.post('course/detail', {
						id: this.id
					}, {
						token: true
					});
					this.courseData = res.data
					this.type = res.data.type
					this.catalogue_list = res.data.catalogue_list
				} catch (error) {
					console.error('Error fetching IndexData:', error);
				} finally {
					uni.hideLoading();
				}
			},
			openvideo(id){
				uni.navigateTo({
					url: '/course/study/index?course_id=' + this.id + '&cate_id=' + id + '&type=' + this.courseData.type
				})
			},
			async topay() {
				try {
					const res = await $H.post('order/CreateCourseOrder', {
						id: this.id,
						pay_way: this.pay_way,
					}, {
						token: true
					});
					
					this.paypop = false
					if (res.code == 1) {
						if (res.data.code == 1) {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
							setTimeout(() => {
								uni.navigateTo({
									url: '/order/course/index'
								})
							}, 400)
						} else if(res.data.code == 2){
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
							// this.paypop = false
						} else {
							uni.requestPayment({
								provider: 'wxpay',
								appId: res.data.config.appId,
								timeStamp: res.data.config.timeStamp,
								nonceStr: res.data.config.nonceStr,
								package: res.data.config.package,
								signType: res.data.config.signType,
								paySign: res.data.config.paySign,
								success: function(res) {
									//跳转到会话页面
									setTimeout(() => {
										uni.navigateTo({
											url: '/order/course/index'
										})
									}, 400)
								},
								fail: function(err) {
									uni.showToast({
										title: '支付失败',
										duration: 2000
									});
								}
							})
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				} catch (error) {
					console.error('Error fetching IndexData:', error);
				} finally {
					uni.hideLoading();
				}
			},
			Tourl(url) {
				uni.navigateTo({
					url: url
				})
			},
			muluchange(e) {
				this.btnnum = e
			},
			async collect(type) {
				try {
					const res = await $H.post('course/addCollect', {
						id: this.id,
						collect: type
					}, {
						token: true
					});
					this.IndexData()
				} catch (error) {
					console.error('Error fetching IndexData:', error);
				} finally {
					uni.hideLoading();
				}
			},
		},
	}
</script>

<style lang="less" scoped>
	page {
		background-color: #f3f3f3;
		font-family: SimHei;
	}

	.container {
		background-color: #f3f3f3;
	}

	// 视频部分
	.shipinpart {
		// height: 640upx;
		background-color: #fff;

		.shipinpart-media {
			height: 12.5rem;
			background-color: #fff;
		}

		&-media {
			height: 400upx;
			background-color: #fff;

			.video {
				width: 750upx;
				height: 400upx;
			}

			.audio_bg {
				position: absolute;
				z-index: -19;
				transition: all .4s cubic-bezier(0.42, 0, 0.58, 1) 0s;
			}

			.donghua(@DHname) {
				@keyframes @DHname {
					0% {
						filter: blur(0upx);
					}

					5% {
						filter: blur(1upx);
					}

					10% {
						filter: blur(2upx);
					}

					15% {
						filter: blur(3upx);
					}

					20% {
						filter: blur(4upx);
					}

					25% {
						filter: blur(5upx);
					}

					30% {
						filter: blur(6upx);
					}

					35% {
						filter: blur(7upx);
					}

					40% {
						filter: blur(8upx);
					}

					45% {
						filter: blur(9upx);
					}

					50% {
						filter: blur(10upx);
					}

					55% {
						filter: blur(9upx);
					}

					60% {
						filter: blur(8upx);
					}

					65% {
						filter: blur(7upx);
					}

					70% {
						filter: blur(6upx);
					}

					75% {
						filter: blur(5upx);
					}

					80% {
						filter: blur(4upx);
					}

					85% {
						filter: blur(3upx);
					}

					90% {
						filter: blur(2upx);
					}

					95% {
						filter: blur(1upx);
					}

					100% {
						filter: blur(0upx);
					}
				}
			}

			;
			.donghua(myDongHua);

			.animation(@animation-name, @animation-duration, @animation-iteration-count) {
				animation: @arguments;
			}

			.gaosi_filter {
				.animation(myDongHua, 4s, infinite);
				transition: all 4s cubic-bezier(0.42, 0, 0.58, 1) 0s;
			}

			image {
				width: 100%;
				height: 400upx;
			}

			.audio {
				width: 100%;
				height: 400upx;
				position: relative;
				display: flex;
				justify-content: center;
				align-items: center;
				z-index: 19;
				padding-bottom: 20upx;

				.bofang {
					width: 80upx;
					height: 80upx;

					image {
						width: 80upx;
						height: 80upx;
					}
				}

				&-wrapper {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					position: absolute;
					bottom: 0;
					color: #333;
					background-image: linear-gradient(to bottom, rgba(255, 255, 255, .25), #fff);

					.prev,
					.next {
						width: 48upx;
						height: 44upx;
						margin: 0 20upx;
					}

					.audio-number {
						font-size: 24upx;
					}

					.audio-slider {
						flex: 1;
					}
				}
			}
		}

		&-info {
			z-index: 19;
			padding: 10px;
			display: flex;
			flex-direction: column;
			margin-top: 15px;
			margin-bottom: 15px;
			.info-span1 {
				margin-top: 35upx;
				font-size: 32upx;
				font-weight: 700;
				color: #070707;
				letter-spacing: 5upx;
			}

			.info-span2 {
				letter-spacing: 5upx;
				font-size: 26upx;
				color: #373737;
				margin-top: 16upx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				text:nth-child(2) {
					letter-spacing: 1upx;
					width: 110upx;
					height: 43upx;
					border-radius: 10upx;
					background-color: #e4edff;
					color: #4b89ff;
					text-align: center;
					line-height: 43upx;
					margin-left: 50upx;
					margin-right: 30upx;
					box-shadow: 0upx 2upx 3upx 1upx #8dbeff;
				}
			}

			.info-span5 {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			.info-span3 {
				margin-top: 18upx;
				font-size: 24upx;
				color: #b3b3b3;
				display: flex;
				align-items: center;

				text:nth-child(2) {
					margin: 0 15upx;
				}
			}

			.info-span4 {
				margin-top: 22upx;
				color: #ff6229;
				font-size: 32upx;
				margin-right: 40upx;
			}
		}
	}


	// 课程部分
	.kechengpart {
		margin-top: 10upx;
		&-title {
			height: 125upx;
			// background-color: red;
			background-color: #fff;
			box-sizing: border-box;
			display: flex;
			justify-content: space-around;
			align-items: center;
			&-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;

				view {
					font-size: 30upx;
					color: #575757;
				}

				.btna {
					// 需要追加到view上的class
					font-weight: 700;
					color: #131313;
				}

				._underline {
					// 需要追加到view下方的下划线class
					width: 70upx;
					height: 7upx;
					background-color: #2f77ff;
					border-radius: 5upx;
					margin-top: 15upx;
				}
			}

		}

		&-content {

			// 课程简介
			.kcjs {
				padding: 0 20upx;
				background-color: #fff;
				display: none;

				&-lecturer {
					&-top {
						height: 60upx;
						display: inline-flex;

						image {
							width: 47upx;
							height: 33upx;
							margin-top: 4upx;
						}

						text {
							font-size: 26upx;
							font-weight: 700;
							color: #020202;
							margin-left: 15upx;
						}
					}

					.jiangshi-right {
						display: inline-flex;
						align-items: center;
						float: right;

						text {
							font-size: 22upx;
							color: #6d6d6d;
							margin-right: 0;
						}

						image {
							width: 20upx;
							height: 22upx;
							margin-left: 5upx;
						}
					}

					&-bottom {
						display: flex;
						align-items: center;
						justify-content: space-between;

						image {
							width: 100%;
							height: 400upx;
						}

						.jiangshi-left {
							display: flex;
							align-items: center;

							image {
								width: 80upx;
								height: 80upx;
								border-radius: 40upx;
							}

							text {
								font-size: 25upx;
								color: #333;
								margin-left: 20upx;
							}
						}

						.jiangshi-right {
							display: flex;
							align-items: center;

							text {
								font-size: 26upx;
								color: #C0C0C0;
								margin-right: 0;
							}

							image {
								width: 20upx;
								height: 26upx;
								margin-left: 5upx;
							}
						}

					}
				}

				&-brief {
					// margin-top: 40upx;
					margin-bottom: 150upx;

					&-top {
						height: 75upx;
						display: flex;

						image {
							width: 53upx;
							height: 49upx;
						}

						text {
							font-size: 26upx;
							font-weight: 700;
							color: #020202;
							margin-left: 15upx;
							margin-top: 4upx;
						}
					}

					&-center {
						padding-bottom: 30upx;

						text {
							font-size: 26upx;
							color: #313131;
						}
					}
				}
			}

			// 课程目录
			.kcml {
				display: none;
				padding: 0 20upx;
				background-color: #fff;

				&-list {
					padding: 20upx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					border-bottom: 1rpx solid #eee;

					&-left {
						width: 436upx;
						height: 60upx;
						line-height: 60upx;

						.yinpin {
							display: inline-block;
							width: 64upx;
							height: 32upx;
							font-size: 20upx;
							color: #ff6969;
							border: 2upx solid #ff6969;
							border-radius: 5upx;
							box-sizing: border-box;
							text-align: center;
							line-height: 28upx;
							margin-right: 15upx;
							vertical-align: middle;
						}

						.shipin {
							display: inline-block;
							width: 64upx;
							height: 32upx;
							font-size: 20upx;
							color: #398cff;
							border: 2upx solid #398cff;
							border-radius: 5upx;
							box-sizing: border-box;
							text-align: center;
							line-height: 24upx;
							margin-right: 15upx;
							vertical-align: middle;
						}

						.tuwen {
							display: inline-block;
							width: 64upx;
							height: 32upx;
							font-size: 20upx;
							color: #8bc34a;
							border: 2upx solid #8bc34a;
							border-radius: 5upx;
							box-sizing: border-box;
							text-align: center;
							line-height: 24upx;
							margin-right: 15upx;
							vertical-align: middle;
						}

						text:nth-child(2) {
							font-size: 25upx;
							color: #070707;
							display: inline-block;
							white-space: nowrap;
							width: 80%;
							overflow: hidden;
							text-overflow: ellipsis;
							vertical-align: middle;
						}
					}

					&-right {
						text {
							font-size: 24upx;
							color: #007AFF;
							vertical-align: middle;
						}

						image {
							width: 30upx;
							height: 30upx;
							vertical-align: middle;
						}
					}
				}
			}

			// 用户评价
			.yhpj {
				display: none;
				text-align: center;
				margin-bottom: 140upx;
				background-color: #fff;
				padding: 30rpx 0;

				&-list {
					.item {
						display: flex;
						padding: 0 20upx;
						margin-top: 40upx;

						&-left {
							width: 100upx;
							height: 100upx;
							border-radius: 50%;
							margin-right: 20upx;
						}

						&-right {
							display: flex;
							flex-direction: column;

							&-top {
								display: flex;
								align-items: center;

								text:nth-child(1) {
									font-size: 32upx;
									color: #333;
								}

								text:nth-child(2) {
									font-size: 26upx;
									color: #999;
									margin-left: 20upx;
								}
							}

							&-bottom {
								max-width: 500upx;
								// height: 60upx;
								margin-top: 10upx;
								padding: 20upx;
								border-radius: 10upx;
								background-color: #fff;
								text-align: left;

								text {
									font-size: 30upx;
									font-weight: 400;
									color: #333;
								}
							}
						}
					}
				}
			}

			.nopl {
				width: 334upx;
				height: 243upx;
				margin-top: 20upx;
			}

			.dis {
				display: block;
			}
		}
	}


	.buy {
		width: 100%;
		height: 122upx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		border-radius: 40upx 40upx 0 0;
		border-top: 1rpx solid #eee;

		&-left {
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;
			align-items: center;

			image {
				width: 37upx;
				height: 37upx;
			}

			text {
				margin-top: 15upx;
				font-size: 24upx;
				color: #ff6229;
			}
		}

		.sharebtn {
			margin: 0;
			padding: 0;
			outline: none;
			border-radius: 0;
			background-color: transparent;
			line-height: inherit;
			width: max-content;
		}

		.sharebtn:after {
			border: none;
		}

		&-right {
			width: 450upx;
			height: 80upx;
			background-image: linear-gradient(to right, #4498ff, #1763ff);
			border-radius: 80upx;
			font-size: 34upx;
			font-weight: 700;
			color: #fff;
			// border: 3upx solid #fff;
			text-align: center;
			line-height: 80upx;
			letter-spacing: 7rpx;
		}
	}
	
	.showbottom {
		padding: 20px;
	}
	
	.pay-type-list {
		// margin: 15px;
		margin-bottom: 10px;
		.pay-title {
			height: 70upx;
			font-size: 14px;
			line-height: 70upx;
			font-weight: bold;
			// border-bottom: 2upx solid #DDDAD4;
		}
	
		.type-item {
			height: 110upx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 30upx;
			position: relative;
			padding: 10px;
			margin-bottom: 15px;
			// border-bottom: 2upx solid #DDDAD4;
			.a-img {
				width: 70upx;
				height: 70upx;
				margin-right: 20upx;
			}
		}
	
		.b-b {
			border-bottom: 2upx solid #DDDAD4;
		}
	
		.icon {
			width: 100upx;
			font-size: 52upx;
		}
	
		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}
	
		.icon-weixinzhifu {
			color: #36cb59;
		}
	
		.icon-alipay {
			color: #01aaef;
		}
	
		.tit {
			font-size: 28upx;
			color: #f97020;
			margin-bottom: 4upx;
		}
	
		.con {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: 28upx;
			color: #f97020;
		}
	}
	
	.act{
		background: rgba(0, 122, 255, 0.1);
		border-radius: 15px;
	}
	
	.text110 {
		color: rgba(255, 255, 255, 1);
		font-size: 28rpx;
		font-family: PingFangSC-Regular;
	}
	
	
	.botton {
		bottom: 20px;
		position: fixed;
		width: 90%;
		border-radius: 44rpx;
		background-color: var(--color-primary);
		height: 50px;
		justify-content: center;
		align-items: center;
		display: flex;
		flex-direction: column;
	}
	
</style>